using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using GauntletFramework.StateManagment;
using GauntletFramework.StateManagment.Menu;
using GauntletFramework.StateManagment.Menu.Menus;
using GauntletFramework.Input;

namespace GauntletFramework
{
    public class GNTMainGame : Microsoft.Xna.Framework.Game
    {
        public GraphicsDeviceManager graphics;
        public StateStack StateStack;
        public BasicInput BasicInput;
        public SpriteBatch SpriteBatch;
        public Texture2D MonoColor;//used for fade

        public GNTMainGame()
        {
            //setup resoultion to suport WP7 & XBox & Win
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 480;
            graphics.SupportedOrientations = DisplayOrientation.Default | DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight | DisplayOrientation.Portrait;
            graphics.ApplyChanges();

            this.IsMouseVisible = true;

            Content.RootDirectory = "Content";

            //Set frame rate is 30 fps
            TargetElapsedTime = TimeSpan.FromTicks(333333);
        }

        protected override void Initialize()
        {
            MonoColor = Content.Load<Texture2D>(@"GauntletBase\MonoColor");

            SpriteBatch = new SpriteBatch(GraphicsDevice);

            StaticGame.Game = this;

            BasicInput = new BasicInput();

            // Create the screen manager component.
            StateStack = new StateStack();
            StateStack.PushState(new MainMenu());
            Components.Add(StateStack);
            //StateStack.add mainmenu

            //basic input is added after statestack to it is drawn after
            Components.Add(BasicInput);

            base.Initialize();
        }

        protected override void  Update(GameTime gameTime)
        {
            if (StateStack.Count <= 0)
            {
                Exit();
                return;
            }

 	        base.Update(gameTime);
        }
        
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            StaticGame.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);

            base.Draw(gameTime);

            Rectangle ScreenSize = StaticGame.ScreenSize;

            Rectangle Top = new Rectangle(ScreenSize.X - 1000, ScreenSize.Y - 1000, ScreenSize.Width + 2000, 1000);
            SpriteBatch.Draw(StaticGame.Game.MonoColor, Top, Color.Black);
            Rectangle Bottom = new Rectangle(ScreenSize.X - 1000, ScreenSize.Y + ScreenSize.Height, ScreenSize.Width + 2000, 1000);
            SpriteBatch.Draw(StaticGame.Game.MonoColor, Bottom, Color.Black);
            Rectangle Left = new Rectangle(ScreenSize.X - 1000, ScreenSize.Y - 1000, 1000, ScreenSize.Height + 2000);
            SpriteBatch.Draw(StaticGame.Game.MonoColor, Left, Color.Black);
            Rectangle Right = new Rectangle(ScreenSize.X + ScreenSize.Width, ScreenSize.Y - 1000, 1000, ScreenSize.Height + 2000);
            SpriteBatch.Draw(StaticGame.Game.MonoColor, Right, Color.Black);

            SpriteBatch.End();
        }
    }
}
